﻿using System.Linq;
using RehabilitationMvc.Models;

namespace RehabilitationMvc.DataAccess
{
    public class RehabilitationRepository: IRehabilitationRepository
    {
        private readonly RehabilitationDbContext _db = new RehabilitationDbContext();

        public void Dispose()
        {
            _db.Dispose();
        }

        public IQueryable<Review> AllReviews()
        {
            return _db.Reviews;
        }

        public Review GetReview(int id)
        {
            return _db.Reviews.Find(id);
        }

        public void AddReview(Review review)
        {
            _db.Reviews.Add(review);
            _db.SaveChanges();
        }

        public void UpdateReview(Review review)
        {
            var original = GetReview(review.Id);
            _db.Entry(original).CurrentValues.SetValues(review);
            _db.SaveChanges();
        }

        public void DeleteReview(int id)
        {
            var review = GetReview(id);
            _db.Reviews.Remove(review);
            _db.SaveChanges();
        }
    }
}